package data;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import properties.DefaultProperties;

/**
 * The class provides the table model for Ultimate File Renamer tool to list the
 * files for renaming.
 * 
 * @author Kevin Wagner
 * @version 1.0
 */
public class FileTableModel extends AbstractTableModel {

	private static final long serialVersionUID = -7178654727792224960L;
	private final String[] columns;
	private ArrayList<String[]> data = new ArrayList<String[]>();

	/**
	 * Creates a new Tablemodel object.
	 * 
	 * @param props
	 *            Properties
	 */
	public FileTableModel(DefaultProperties props) {
		columns = new String[] { "#", props.getLocalizedText("fileName"),
				props.getLocalizedText("patternIn"),
				props.getLocalizedText("patternOut"),
				props.getLocalizedText("output"),
				props.getLocalizedText("type")};
		data.clear();
	}

	@Override
	public int getColumnCount() {
		return columns.length;
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	@Override
	public Object getValueAt(int row, int column) {
		if(column==0){
			return row+1;
		}
		return data.get(row)[column-1];
	}

	@Override
	public Class<?> getColumnClass(int column) {
		return String.class;
	}

	@Override
	public String getColumnName(int column) {
		return columns[column];
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return false;
	}

	/**
	 * Adds the given row to the table model.
	 * 
	 * @param newData
	 *            the data for the new Row
	 */
	public void addRow(String[] newData) {
		data.add(newData);
		fireTableRowsInserted(data.size(), data.size());
	}

	/**
	 * Adds the given row to the table model.
	 * 
	 * @param task
	 *            the task to add
	 */
	public void addRow(Task task) {
		data.add(new String[] { task.getInputFileName(),
				task.getInputPattern(), task.getOutputPattern(),
				task.getOutputFileName(),task.getSuffix()});
		fireTableRowsInserted(data.size(), data.size());
	}

	/**
	 * Removes all elements from the model.
	 */
	public void clear() {
		data.clear();
	}

	/**
	 * Removes the row with the given index from the model.
	 * 
	 * @param index
	 *            the index of the row
	 */
	public void removeRow(int index) {
		data.remove(index);
		fireTableRowsDeleted(index, index);
	}
	
	/**
	 * 
	 */
	public void update(){
		fireTableDataChanged();
		fireTableStructureChanged();
	}

}
